const app = getApp()
const uploadOss = require('../../utils/upload/uploadImg.js')
import { getClassificationMaterialList, saveFile } from '../../api/index'

Page({
  data: {
    filterHeight: 0,
    firstId: '',
    secendId: '',
    threeId: '',
    fourId: '',
    lastId: '',
    currentPage: 1,
    pageSize: 20,
    list: [],
    isEmpty: false,
    showFilter: false,
    refresher: false
  },
  async onRefresh () {
    await this.getList()
    this.setData({ refresher: false })
  },
  showFilterPop () {
    this.setData({ showFilter: true })
  },
  confirmFilter ({ detail: { firstId, secendId, threeId, fourId, lastId } }) {
    this.setData({
      firstId: firstId,
      secendId: secendId,
      threeId: threeId,
      threeId: threeId,
      lastId: lastId,
      showFilter: false
    })
    this.getList()
  },
  focusInput () {
    wx.$nav.navigateTo('/pages/search/index')
  },
  async chooseMedia () {
    try {
      const { tempFiles } = await wx.$pro.chooseMedia({ count: 1, mediaType: 'image' })
      wx.showLoading({ title: '正在上传图片...' })
      const { fileUrl } = await this.uploadFile(tempFiles[0], 'tempFilePath')
      const { ResultSet } = await saveFile({ fileList: [{ fileUrl: fileUrl }] })
      wx.hideLoading()
      wx.$nav.navigateTo(`/pages/searchImage/index?fileId=${ResultSet.userData.fileIdList[0].fileId}&fileUrl=${ResultSet.userData.fileIdList[0].fileUrl}`)
    } catch (err) {
      wx.hideLoading()
      console.log(err)
    }
  },
  uploadFile (item, key, files = '', index = '', that = '', dataKey = '') {
    return new Promise((resolve, reject) => {
      uploadOss(item[key],
        (res) => {
          resolve(res)
        },
        (err) => {
          resolve()
        },
        files, index, that, dataKey
      )
    })
  },
  cancelFilter () {
    this.setData({ showFilter: false })
  },
  async getList () {
    try {
      const { ResultSet } = await getClassificationMaterialList({ currentPage: 1, pageSize: this.data.pageSize, classificationId: this.data.lastId })
      this.loading = false
      this.alloaded = false
      if (ResultSet.userData.data.length) this.data.currentPage = 2
      this.data.isEmpty = ResultSet.userData.data.length && ResultSet.userData.data.length < this.data.pageSize
      this.setData({
        list: ResultSet.userData.data,
        currentPage: this.data.currentPage,
        isEmpty: this.data.isEmpty
      })
      this.loading = false
    } catch (err) {
      this.loading = false
      wx.showToast({ title: err.message || err.ResultSet.errors })
    }
  },
  async loadMore () {
    try {
      if (this.loading || this.alloaded) return
      this.loading = true
      const { ResultSet } = await getClassificationMaterialList({ currentPage: this.data.currentPage, pageSize: this.data.pageSize, classificationId: this.data.lastId })
      this.alloaded = !ResultSet.userData.data.length
      this.data.isEmpty = !ResultSet.userData.data.length
      this.setData({ isEmpty: this.data.isEmpty })
      if (ResultSet.userData.data.length) {
        this.data.currentPage = this.data.currentPage + 1
        this.data.list.push(...ResultSet.userData.data)
        this.setData({
          list: this.data.list,
          currentPage: this.data.currentPage
        })
      }
      this.loading = false
    } catch (err) {
      console.log(err)
      this.loading = false
      wx.showToast({ title: err.message || err.ResultSet.errors })
    }
  },
  onLoad (query) {
    let document = this.createSelectorQuery()
    document.select('.material-filter').boundingClientRect(rect=>{
      let height = rect.height
      this.setData({ filterHeight: height })
    }).exec()
    wx.setNavigationBarTitle({ title: query.title })
    this.setData({
      firstId: query.first,
      secendId: query.secend,
      lastId: query.secend
    })
    this.getList()
  }
})